package sbc.corso;

import java.util.*;
import corso.lang.*;

import sbc.fact.*;
import sbc.repos.*;

public class ChannelConsumer implements Runnable {
        
    //private CorsoChannel channel;
    private Worker worker;
    private Thread currentThread;
    private String site;
    private CorsoConstOid channelOid;

    public ChannelConsumer(Worker w, CorsoConstOid chnlOid, String sit) {
        channelOid = chnlOid;
        worker = w;
        site = sit;
    }
    public void start() {
        if(currentThread == null){
            currentThread = new Thread(this);
            currentThread.start();
        }
    }

    public void stop() { currentThread = null; }

    public void run() {
        while(currentThread != null) {
            try {
                
                CorsoChannel channel = new CorsoChannel(channelOid);
                CorsoVarOid oid = channel.next();
                System.out.println("got next oid from channel: " + oid);
                
                System.out.println("Oid received: " +oid);
                worker.workOn(oid);
                
                //for testing
                Thread.currentThread().sleep(1000);
                

                // create the channel object ********************************************
                
            } catch(Exception e) {
                System.out.println("Couldn't retrieve Oid from channel");
                System.out.println(e);
            }   

        }
    }
}
   
